package techguy;

import java.util.Date;
import java.util.List;
import java.util.ResourceBundle;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.ArrayDataModel;
import javax.faces.model.DataModel;

import beans.TechPageBean;
import dtos.TechresourceDTO;
import dtos.TechticketDTO;
import dtos.TechticketHistoryDTO;
import dtos.UserDTO;
import utils.TechresourceDAORemote;
import utils.TechticketDAORemote;
import utils.UserDAORemote;

@ManagedBean
@ViewScoped
public class RequestBean {
	/**
	 * @author andreicl
	 * Techticket the tech guy sends to admin
	 */
	@EJB
	TechresourceDAORemote techResource;
	@EJB
	TechticketDAORemote ticket;
	
	ResourceBundle bundle = ResourceBundle.getBundle(
			"languageSettings.language", FacesContext.getCurrentInstance()
					.getViewRoot().getLocale());
	
	/**
	 * remove this user and replace with session user
	 */
	@EJB
	UserDAORemote user;
	private UserDTO userDTO;
	private int idDomain = -1;

	private String description, problemType;
	private int resourceId = -1;
	private String detail;
	private DataModel<TechticketHistoryDTO> ticketDataModel;
	private TechticketDTO techTicketDTO;
	private TechresourceDTO techResourceDTO;
	private List<TechresourceDTO> resources;
	private TechticketHistoryDTO[] tickets;
	
	@ManagedProperty("#{techPageBean}")
	TechPageBean tpb;

	@PostConstruct
	public void init() {
		techTicketDTO = new TechticketDTO();
		techTicketDTO.setDate(new Date());
		techTicketDTO.setStatus("Processing");
		techTicketDTO.setProblemtype("Replacement");
		//replace here
		//userDTO = user.getUser(2);
		userDTO = (UserDTO) FacesContext.getCurrentInstance()
				.getExternalContext().getSessionMap().get("loggedUser");
		techTicketDTO.setIdUser(userDTO.getId());
		idDomain = userDTO.getIdDomain();
		ticketDataModel = new ArrayDataModel<>(tickets);
		if (getResources() != null) {
			techResourceDTO = getResources()[0];
			this.setDetail(bundle
					.getString("BU_createNotification_notificationTable_detail1")
					+ techResourceDTO.getName()
					+ bundle.getString("BU_createNotification_notificationTable_detail2")
					+ techResourceDTO.getIdnumber()
					+ bundle.getString("BU_createNotification_notificationTable_detail3")
					+ techResourceDTO.getIp()
					+ bundle.getString("BU_createNotification_notificationTable_detail4")
					+ techResourceDTO.getStatus());
		}
	}

	/**
	 * convert the ArrayList to Array
	 */
	public TechresourceDTO[] getResources(){
		resources = techResource.getTechResourcesDTOByDomainId(idDomain);
		return resources.toArray(new TechresourceDTO[resources.size()]);
	}

	/**
	 * 
	 * @param event
	 * 
	 * used in GUI for displaying details about the selected resource
	 */
	public void valueChange(ValueChangeEvent event) {
		techResourceDTO = (TechresourceDTO) techResource
				.getTechresourcesById((Integer) event.getNewValue());

	
		this.setDetail(bundle.getString("TG_createRequest_notificationTable_detail1")
				+ techResourceDTO.getName() + bundle.getString("TG_createRequest_notificationTable_detail2")
				+ techResourceDTO.getIdnumber() + bundle.getString("TG_createRequest_notificationTable_detail3")
				+ techResourceDTO.getIp() + bundle.getString("TG_createRequest_notificationTable_detail4")
				+ techResourceDTO.getStatus());
		techTicketDTO.setIdTechresource(techResourceDTO.getId());
	}

	/**
	 * 
	 * @param event
	 * used for inserting a new Teckticket in DB when a button is pushed in GUI and reseting the input fields
	 */
	public void insertTechTicket(ActionEvent event) {
		ticket.insertTechticket(techTicketDTO);
		techResourceDTO.setStatus("Not Working");
		techResource.updateTechresource(techResourceDTO);
		description = "";
		problemType = "";
		tpb.setPage("ticketView");
	}
	
	/**
	 * 
	 * @return DataModel<TechticketHistoryDTO>
	 * used for line numbering in dataTable in GUI
	 */
	public DataModel<TechticketHistoryDTO> getTickets() {
		return ticketDataModel;
	}

	public String getDetail() {
		return detail;
	}

	public void setDetail(String detail) {
		this.detail = detail;
	}

	public int getResourceId() {
		return resourceId;
	}

	public void setResourceId(int resourceId) {
		this.resourceId = resourceId;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		techTicketDTO.setDescription(description);
		this.description = description;
	}

	public String getProblemType() {
		return problemType;
	}

	public void setProblemType(String problemType) {
		techTicketDTO.setProblemtype(problemType);
		this.problemType = problemType;
	}
	public TechPageBean getTpb() {
		return tpb;
	}

	public void setTpb(TechPageBean tpb) {
		this.tpb = tpb;
	}



}
